package org.standard.dic.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.standard.commons.exception.InfrastructureException;
import org.standard.commons.utils.DAO;
import org.standard.dic.model.Dic;
import org.standard.dic.service.DicService;
import org.standard.sys.model.SysLog;

/**
 * 该类用于处理系统代码
 * 
 * @author wang zitao
 * 
 */

public class DicServiceImpl implements DicService {
	private static final char SUCCESS = '1';  //修改成功
	private static final char FAIL = '0';    //修改失败
	private static final char REPEAT = '2';    //存在重复数据
	private DAO dao;

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	/**
	 * 查找系统代码主分类
	 * 
	 * @exception InfrastructureException
	 * @return
	 */
	@Override
	public List<Dic> searchAllTopCode() {
		List diclist = dao.find("from Dic where dicNoParent is null or dicNoParent='' order by dicOrder ");
		if (diclist.size() > 0) {
			return diclist;
		} else {
			return new ArrayList();
		}
	}
	
	
	/**
	 * 查找系统主分类代码的编号和名称
	 * 
	 * @exception InfrastructureException
	 * @return
	 */
	@Override
	public Dic searchTopCodeByNo(String dicNo) {
		List diclist = dao.find("from Dic where dicNo='"+dicNo+"' order by dicOrder");
		if (diclist.size() > 0) {
			Dic code=(Dic)diclist.get(0);
			return code;
		} else {
			return new Dic();
		}
	}

	/**
	 * 通过dicId查找自定义代码
	 * 
	 * @exception InfrastructureException
	 * @return dic
	 */
	public Dic searchCodeById(String dicId) {
		List diclist = dao.find("from Dic where dicId='"+dicId+"'");
		if (diclist.size() > 0) {
			Dic code=(Dic)diclist.get(0);
			return code;
		} else {
			return new Dic();
		}
	}

	
	
	/**
     * 通过系统编号查找系统分类代码
     * 用于编辑代码
	 * @author wang zitao
	 * @prama dicno本级代码编号
	 * @return 代码信息
	 */

	@Override
	public List<Dic> searchCodeByNo(String dicno,String dicNoParent) {
		String hql="";
		List dics=null;
		if(dicNoParent!=null&&!"".equals(dicNoParent)){
			hql="from Dic where dicNo='"+dicno+"' and dicNoParent='" + dicNoParent+"' order by dicOrder";
			dics = dao.find(hql);
		}
		else{
			hql="from Dic where (dicNo =? and dicNoParent='') or (dicNo =? and dicNoParent is null) order by dicOrder";
			dics = dao.find(hql,new Object[]{dicno,dicno});
		}
		if (dics.size() > 0) {
			return dics;
		} else {
			return new ArrayList();
		}
	}

	
	/**
     * 通过传入的上级系统代码编号查找该代码下的所有下级代码
	 * @author wang zitao
	 * @prama codeno本级代码编号，在查询时与副代码相对应
	 * @return 代码信息列表
	 */

	@Override
	public List<Dic> searchNextCodeByNo(String dicno) {
		String hql="";
		if(dicno!=null&&!"".equals(dicno)){
			hql="from Dic where dicNoParent='" + dicno+"' order by dicOrder ";
		}
		List dics = dao.find(hql);
		if (dics.size() > 0) {
			return dics;
		} else {
			return new ArrayList();
		}
	}
	
	
	/**
	 * 修改代码信息
	 * 
	 * @param dic
	 *            Dic对象
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wang zitao
	 */
	@Override
	public char modifyCode(Dic dic) {
		try {
			List dics=new ArrayList();
			if(dic.getDicNoParent()!=null&&!"".equals(dic.getDicNoParent())){
//				dics=dao.find("select dicId from Dic where dicNo<>? and dicNoParent=? and dicName=?",
//						new Object[]{dic.getDicNo(),dic.getDicNoParent(),dic.getDicName()});
			}
			else{
				dics=dao.find("select dicId from Dic where dicId<>'"+dic.getDicId()+"'  and dicName ='"+
						dic.getDicName()+"' and dicNo='"+dic.getDicNo()+"' and (dicNoParent is null or dicNoParent='')");
			}
				
			if(null==dics||dics.size()==0){
				dao.update(dic);
			}
			else
			    return FAIL;
			return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}

	/**
	 * 添加系统代码
	 * 
	 * @param dic
	 *            Dic对象
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char createCode(Dic dic) {
		String  message="";
		try {
			List dics=dao.find("from Dic where (dicNo=? and dicNoParent is null)" +
					" or (dicNo=? and dicNoParent ='') or (dicName=? and dicNoParent is null) or (dicName=? and dicNoParent ='')",
					 new Object[]{dic.getDicNo(),dic.getDicNo(),dic.getDicName(),dic.getDicName()});
			if(dics.size()>0){
				  return REPEAT;
			}
			String dicId = (String) dao.save(dic);
			return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}
	
	/**
	 * 添加二级系统代码
	 * 
	 * @param dic
	 *            Dic对象
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char createNextCode(Dic dic) {
		String  message="";
		//查找是否有代码重复的
		try {
			List dics=dao.find("from Dic where (dicNo=? and dicNoParent=?) or (dicName=? and dicNoParent=?) ",
					new Object[]{dic.getDicNo(),dic.getDicNoParent(),dic.getDicName(),dic.getDicNoParent()});
			if(dics.size()>0){
				  return REPEAT;
			}
			String dicId = (String) dao.save(dic);
			return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}

	/**
	 * 删除主代码类
	 * 
	 * @param dicNo
	 *            要删除的代码编号
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char deleteCode(String dicId,String dicNo) {
		try {
				dao.updateBulk("delete from Dic where (dicNo=? and dicNoParent is null) or (dicNo=? and dicNoParent='') or dicNoParent=?", new Object[]{dicNo,dicNo,dicNo});
				return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}
	
	
	
	/**
	 * 删除二级代码类
	 * 
	 * @param dicId
	 *            要删除的代码序号
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char deleteNextCode(String dicId) {
		try {
		 dao.delete(Dic.class, dicId);
		return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}
}